package com.content.Controller.drama;

import com.common.Entity.Result;
import com.common.Utils.JwtUtil;
import com.content.Entity.drama.BookMake;
import com.content.Entity.drama.Drama;
import com.content.Service.SysService;
import com.content.Service.drama.DramaService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/drama")
public class DramaController {
    @Autowired
    private DramaService dramaService;
    @Autowired
    private SysService sysService;
    @GetMapping("/getDramaList")
    public Result getDramaList(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(required = false) String dramaName,
                               @RequestParam(required = false) String token){
        if (!Objects.equals(token, "")) {
            String username = JwtUtil.extractUsername(token);
            if (username == null) {
                return Result.error("无效的token");
            }
            PageInfo<Drama> pageInfo= dramaService.getDramaListByThree(pageNum, pageSize, dramaName,username);
            return Result.success(pageInfo);
        }
        PageInfo<Drama> pageInfo= dramaService.getDramaList(pageNum, pageSize, dramaName);
        return Result.success(pageInfo);
    }

    @GetMapping("/getDrama/{dramaId}")
    public Result getDrama(@PathVariable String dramaId){
        Drama drama = dramaService.getDrama(dramaId);
        return Result.success(drama);
    }

    @PostMapping("/addDrama")
    public Result addDrama(@RequestBody Drama drama){
        dramaService.addDrama(drama);
        return Result.success();
    }

    @PutMapping("/updateDrama")
    public Result updateDrama(@RequestBody Drama drama){
        dramaService.updateDrama(drama);
        return Result.success();
    }

    @DeleteMapping("/deleteDrama/{dramaId}")
    public Result deleteDrama(@PathVariable String dramaId){
        dramaService.deleteDrama(dramaId);
        return Result.success();
    }

    @GetMapping("/getFavorites")
    public Result getFavorites(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(required = false) String sortType,
                               @RequestParam String token){
        String username = JwtUtil.extractUsername(token);
        String userId=sysService.getUserByName(username);
        PageInfo<BookMake> pageInfo=dramaService.getFavorites(pageNum, pageSize,sortType,userId);
        return Result.success(pageInfo);
    }

    @GetMapping("/favorites/{token}")
    public Result favorites(@PathVariable("token") String token){
        String username = JwtUtil.extractUsername(token);
        String userId=sysService.getUserByName(username);
        List<BookMake> bookMakes=dramaService.getBookMakes(userId);
        return Result.success(bookMakes);
    }

    @PostMapping("/favorite")
    public  Result favorite(@RequestBody BookMake bookMake){
        String token = bookMake.getToken();
        String username = JwtUtil.extractUsername(token);
        String userId=sysService.getUserByName(username);
        bookMake.setUserId(userId);
        dramaService.addBookMake(bookMake);
        return Result.success();
    }

    @PostMapping("/unfavorite")
    public Result unfavorite(@RequestBody BookMake bookMake){
        String token = bookMake.getToken();
        String username = JwtUtil.extractUsername(token);
        String userId=sysService.getUserByName(username);
        bookMake.setUserId(userId);
        dramaService.deleteBookMake(bookMake);
        return Result.success();
    }

}
